package com.jiyici;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView.OnEditorActionListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.jiyici.controller.DataManager;
import com.jiyici.data.ClientDBHelper;
import com.jiyici.data.ClientSyncManager;
import com.jiyici.model.Category;
import com.jiyici.utils.StringUtils;
import com.jiyici.utils.Utils;
import com.jiyici.view.DialogWrapper;
import com.jiyici.view.DialogWrapper.DialogAction;

public class DrawerAdapter extends BaseAdapter implements OnItemClickListener {

    private static final int ITEM_ACCOUNT = 0;
    private static final int ITEM_ITEMS = 1;
    private static final int ITEM_CATEGORY = 2;
    private static final int ITEM_SETTING = 3;
    private static final int ITEM_DISCOVERY = 4;
    private static final int ITEM_HELP = 5;
    private static final int ITEM_ABOUT = 6;
    private static final int ITEM_SYNC = 7;

    private static final int COUNT = 8;

    private Context mContext;
    private LayoutInflater mLayoutInflater;

    public DrawerAdapter(Context context) {
        super();
        this.mContext = context;
        this.mLayoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return COUNT;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = mLayoutInflater.inflate(position != ITEM_ACCOUNT ? R.layout.drawer_item
                : R.layout.drawer_account_item, parent, false);
        ImageView drawerIcon = (ImageView) view.findViewById(R.id.drawer_icon);
        TextView drawerText = (TextView) view.findViewById(R.id.drawer_text);

        switch (position) {
        case ITEM_ACCOUNT:
            drawerIcon.setImageResource(R.drawable.ic_action_person);
            drawerText.setText(DataManager.getUser().email);
            Button btnSignoff = (Button) view.findViewById(R.id.drawer_action);
            btnSignoff.setText(R.string.signoff);
            btnSignoff.setOnClickListener(new View.OnClickListener() {
                
                @Override
                public void onClick(View v) {
                    displaySignoffDialog(mContext);
                }
            });
            break;
        case ITEM_ITEMS:
            drawerText.setText(R.string.item);
            break;
        case ITEM_CATEGORY:
            drawerText.setText(R.string.category);
            break;
        case ITEM_SETTING:
            drawerIcon.setImageResource(R.drawable.ic_action_settings);
            drawerText.setText(R.string.setting);
            break;
        case ITEM_DISCOVERY:
            drawerText.setText(R.string.discovery);
            break;
        case ITEM_HELP:
            drawerIcon.setImageResource(R.drawable.ic_action_help);
            drawerText.setText(R.string.help);
            break;
        case ITEM_ABOUT:
            drawerIcon.setImageResource(R.drawable.ic_action_about);
            drawerText.setText(R.string.about);
            break;
        case ITEM_SYNC:
            drawerText.setText(R.string.sync);
        default:
            break;
        }
        return view;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (position == ITEM_SYNC) {
            new Thread(new Runnable() {
                
                @Override
                public void run() {
                    final boolean dataChanged = ClientSyncManager.sync();
//                    if (dataChanged)
                        DataManager.startLoadData();
                    ((Activity)mContext).runOnUiThread(new Runnable() {
                        
                        @Override
                        public void run() {
                            Toast.makeText(mContext, "sync finshed, datachanged = " + dataChanged, Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }).start();
            Toast.makeText(mContext, "start sync", Toast.LENGTH_SHORT).show();
            return;
        }
        
        if (position == ITEM_ITEMS) {
            ((HomeActivity) mContext).switch2ItemPage();
        } else if (position == ITEM_CATEGORY) {
            ((HomeActivity) mContext).switch2CategoryPage();
        }
        ((HomeActivity) mContext).closeDrawer();
    }
    
    private void displaySignoffDialog(final Context context) {
        final DialogWrapper dialogWrapper = new DialogWrapper(context);
        dialogWrapper.setTitle(R.string.signoff);
        dialogWrapper.setContentView(R.layout.dialogbody_signoff);
        final CheckBox cb_clear_data = (CheckBox) dialogWrapper.getContentView().findViewById(R.id.cb_clear_data);
        DialogAction doneAction = new DialogAction() {

            @Override
            public int run() {
                boolean clearData = cb_clear_data.isChecked();
                if (clearData) {
                    String dbFile = ClientDBHelper.getInstance().getReadableDatabase().getPath();
                    Utils.deleteFile(dbFile);
                }
                ClientDBHelper.getInstance().close();
                DataManager.signoff();
                ((Activity)mContext).finish();
                Intent intent = new Intent();
                intent.setClass(mContext, StartActivity.class);
                mContext.startActivity(intent);
                return DISMISS;
            }
        };
        dialogWrapper.setAction(R.string.signoff, doneAction, R.string.dialog_cancel, null);
        dialogWrapper.show();
    }

}
